package com.shizhanzhe.qdd.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.shizhanzhe.qdd.utils.NetBroadcastReceiver;
import com.shizhanzhe.qdd.widget.ErrorCallback;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zz9527 on 2019/4/13.
 *
 */
public abstract class BaseFragment extends Fragment implements NetBroadcastReceiver.NetChangeListener{
    /**
     *netEvent 网络监听回调
     */
    public static NetBroadcastReceiver.NetChangeListener netEvent;
    protected LoadService loadService;
    Unbinder unbinder;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        View rootView = View.inflate(getActivity(), getLayout(), null);
        unbinder=ButterKnife.bind(this, rootView);
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                getData();
            }
        });
        return loadService.getLoadLayout();
    }

    protected abstract int getLayout();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        netEvent = this;
        getData();
    }

    protected abstract void getData();
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    /**
     * 网络状态改变时间监听
     *
     * @param netWorkState true有网络，false无网络
     */
    @Override
    public void onNetChange(boolean netWorkState, int networkType) {
        if (netWorkState) {
            switch (networkType) {
                case NetBroadcastReceiver.NETTYPE_CMNET:
                case NetBroadcastReceiver.NETTYPE_CMWAP:
                    Toast.makeText(getActivity(), "当前使用流量", Toast.LENGTH_SHORT).show();
                    break;
                case NetBroadcastReceiver.NETTYPE_WIFI:
                    Toast.makeText(getActivity(), "当前使用WIFI", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            getData();
        } else {
            Toast.makeText(getActivity(), "当前无网络", Toast.LENGTH_SHORT).show();
        }
    }
}
